package com.avic.rong.richEditor.widget;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.avic.rong.R;

/**
 * Creater: Created by wangyz on 2019/9/24.
 * Email: 936804097@qq.com
 */
public class SuperLinkDialog extends Dialog implements View.OnClickListener {


    private Context context;
    private EditText et_link_name;
    private EditText et_link_address;



    public SuperLinkDialog(Context context) {

        //使用自定义Dialog样式
        super(context, R.style.Theme_dialog);

        this.context = context;
        //指定布局
        setContentView(R.layout.superlink_dialog);

        findViewById(R.id.bt_ok).setOnClickListener(this);
        findViewById(R.id.bt_cancel).setOnClickListener(this);
        findViewById(R.id.img_close).setOnClickListener(this);

        et_link_name = findViewById(R.id.et_link_name);
        et_link_address = findViewById(R.id.et_link_address);

        //点击外部不可消失
        setCancelable(true);
    }


    @Override
    public void onClick(View v) {
        int viewId = v.getId();
        switch (viewId) {
            case R.id.bt_ok:
                String linkName = et_link_name.getText().toString();
                String linkAddress = et_link_address.getText().toString();
                if (TextUtils.isEmpty(linkName)) {
                    Toast toast = Toast.makeText(this.context, "链接名称不能为空", Toast.LENGTH_SHORT);
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();
                    return;
                }
                if (TextUtils.isEmpty(linkAddress)) {
                    Toast toast = Toast.makeText(this.context, "链接地址不能为空", Toast.LENGTH_SHORT);
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();
                    return;
                }

                if(linkCallBackInterface != null){
                    linkCallBackInterface.callBack(linkName, linkAddress);
                }

                dismiss();
                break;
            case R.id.bt_cancel:
            case R.id.img_close:
                dismiss();
                break;
        }
    }

    private  LinkCallBackInterface linkCallBackInterface;

    public void setLinkCallBackInterface(LinkCallBackInterface linkCallBackInterface) {
        this.linkCallBackInterface = linkCallBackInterface;
    }

    public interface LinkCallBackInterface{
        void callBack(String linkName,String linkAddress);
    }
}